/********************************************
 * 功能说明: User实体与DTO之间接口数据转换实现
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.converter;

import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.enums.UserStatus;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.web.dto.WebUserDTO;
import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.enums.UserStatus;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.model.User;
import com.spring.arch.uaa.repository.UserRepository;
import com.spring.arch.uaa.web.dto.WebUserDTO;
import lombok.NonNull;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * User实体与DTO之间接口数据转换实现
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Component
public class WebUserConverter extends AbstractConverter<User, WebUserDTO> {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public User toModel(@NonNull final WebUserDTO dto) {
        if(dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public WebUserDTO toDTO(@NonNull final User model) {
        final WebUserDTO dto = new WebUserDTO();

        dto.setId(model.getId());
        dto.setMobile(model.getMobile());
        dto.setAccount(model.getAccount());
        dto.setRealName(model.getRealName());
        dto.setEmail(model.getEmail());
        dto.setIdentityNo(model.getIdentityNo());
        dto.setGender(model.getGender());
        dto.setStatus(model.getStatus());

        this.convertCommonToDTO(model, dto);

        return dto;
    }

    /**
     * 构建新Model
     */
    private User constructModel(final WebUserDTO dto) {
        final User model = new User();
        model.setMobile(dto.getMobile());
        model.setAccount(dto.getAccount());
        model.setStatus(UserStatus.ACTIVE);
        // 设置密码
        if (StringUtils.isNotBlank(dto.getPassword())) {
            // 生成随机盐
            String salt = RandomStringUtils.randomAlphabetic(6);
            String pwd = passwordEncoder.encode(StringUtils.joinWith("$", dto.getPassword().trim(), salt));
            model.setPassword(pwd);
            model.setSalt(salt);
        }
        this.dtoToModel(dto, model);
        return model;
    }

    /**
     * 更新Model
     */
    private User updateModel(final WebUserDTO dto) {
        final User model = this.userRepository.findById(dto.getId())
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001005001,
                        dto.getAccount()));
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final WebUserDTO dto, final User model) {
        model.setRealName(dto.getRealName());
        model.setEmail(dto.getEmail());
        model.setIdentityNo(dto.getIdentityNo());
        model.setGender(dto.getGender());

    }
}
